package com.example.sellersale.controller;

import com.example.sellersale.entity.*;
import com.example.sellersale.server.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private BillService billService;
    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private CartService cartService;


    /**
     * 实现生成用户订单功能
     * @param cartInfos
     * @return
     */
    @PostMapping("/addOrders")
    public Boolean order(@RequestBody List<CartInfo> cartInfos){
        System.out.println("====前端传递过来的购物车列表=====");
        System.out.println(cartInfos);
        return  orderService.addOrders(cartInfos);
    }
    /**
     * 查询用户购物车信息
     * @param uId
     * @return
     */
    @GetMapping("/userCart")
    public List<CartInfo> userCart(String uId){
        return cartService.userCart(uId);
    }




    /**
     * 添加购物车
     * @param cartInfo
     * @return
     */
    @PostMapping("/addCart")
    public CartInfo addCart(CartInfo cartInfo){
        return cartService.addCartInfo(cartInfo);
    }

    /**
     * 删除购物车
     * @param cartId
     * @return
     */
    @DeleteMapping("/delCart")
    public Boolean delCartInfo(String cartId){
        return cartService.delCartInfo(cartId);
    }

    /**
     * 修改购物车信息
     * @param cartInfo
     * @return
     */
    @PostMapping("/editCart")
    public CartInfo updateCartInfo(CartInfo cartInfo){
        return  cartService.editCartInfo(cartInfo);
    }



    @GetMapping("/goodsTypes")
    public List<GoodsType> goodsTypes(){
        List<GoodsType> list = goodsTypeService.goodsTypes();
        return list;
    }

    /**
     * 实现商品信息展现功能====商品展示
     * @return
     */
    @GetMapping("/scan")
    public List<Goods> showGoods(){
        List<Goods> goods = goodsService.scanGoods();
        return goods;
    }

    /**
     * 支付订单
     * @param bill
     * @return
     */
    @PostMapping("/pay")
    public Bill addBillInfo(Bill bill){
       Bill bill1 =  billService.pay(bill);
        return bill1;
    }

    /**
     * 返回发票信息
     * @param uId 用户id
     * @return 用户所有的发票
     */
    @GetMapping("/bills")
    public List<Bill> userBillInfo(String uId){
        List<Bill> bills = billService.userBillInfo(uId);
        return bills;
    }

    /**
     * 实现登录功能======登录
     * @param user
     * @return
     */
    @GetMapping("/login")
    public User Login(User user){
        return userService.login(user);
    }

    /**
     * 实现个人订单查询功能====订单管理功能
     * @param uId
     * @return
     */
    @GetMapping("/userOrders")
    public List<Order>  ordersSelect(String uId){
        List<Order> orders = orderService.selectOneOrder(uId);
        return orders;
    }

    /**
     * 实现注册功能=====注册
     * @param user
     * @return 新注册的用户个人信息
     */
    @PostMapping("/register")
    public User register(User user){
        System.out.println("新增的用户："+user);
        return userService.register(user);
    }
}
